<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: showclub.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/


  function getSeasonClubCalendar( $clubId, $seasonId, $teams_as_referees = false )
  {
    global $database;
    
    $query = " SELECT m.match_id, m.match_date,
                      m.alt_decision,
                      m.matchpart1_result, m.matchpart2_result,
                      m.matchpart1_result_decision, m.matchpart2_result_decision,
                      t1.id as homeId, t1.name as homeName,
                      t2.id as awayId, t2.name as awayName,
                      pg.name as playgroundName,
                      p.name as projectName ";
    if ( $teams_as_referees ) {
      $query .= ", ref.id as refereeId, ref.name AS refereeName";
    }
    else {
      $query .= ", ref.id as refereeId, CONCAT( ref.firstname, ' ', ref.lastname ) AS refereeName ";    
    }
    
    $query .= " FROM #__joomleague_matches AS m 
               INNER JOIN #__joomleague_teams AS t1 ON t1.id = m.matchpart1
               INNER JOIN #__joomleague_teams AS t2 ON t2.id = m.matchpart2
               LEFT  JOIN #__joomleague_playgrounds AS pg ON pg.id = m.playground_id
               INNER JOIN #__joomleague AS p ON p.id = m.project_id
               INNER JOIN #__joomleague_seasons AS s ON s.id = p.season_id ";
    
    if ( $teams_as_referees ) {
      $query .= "LEFT JOIN #__joomleague_teams AS ref ON ref.id = m.referee_id ";
    }
    else {
      $query .= "LEFT JOIN #__joomleague_referees AS ref ON ref.id = m.referee_id ";   
    }
    
    $query .= " WHERE p.published = 1 
                AND ( t1.club_id = $clubId OR t2.club_id = $clubId ) ";
    
    if ( $seasonId ) {
      $query .= " AND s.id = " . $seasonId;
    }
                 
    $query .= " ORDER BY m.match_date ASC ";

    $database->setQuery( $query );
    // echo $database->getQuery();
    return $database->loadObjectList();
  }
  
  function getSeasonClubRefCalendar( $clubId, $seasonId )
  {
    global $database;
    
    $query = " SELECT m.match_id, m.match_date,
                      m.alt_decision,
                      m.matchpart1_result, m.matchpart2_result,
                      m.matchpart1_result_decision, m.matchpart2_result_decision,
                      t1.id as homeId, t1.name as homeName,
                      t2.id as awayId, t2.name as awayName,
                      pg.name as playgroundName,
                      p.name as projectName ";
    $query .= ", ref.id as refereeId, ref.name AS refereeName";

    
    $query .= " FROM #__joomleague_matches AS m 
               INNER JOIN #__joomleague_teams AS t1 ON t1.id = m.matchpart1
               INNER JOIN #__joomleague_teams AS t2 ON t2.id = m.matchpart2
               LEFT  JOIN #__joomleague_playgrounds AS pg ON pg.id = m.playground_id
               INNER JOIN #__joomleague AS p ON p.id = m.project_id
               INNER JOIN #__joomleague_seasons AS s ON s.id = p.season_id ";
    $query .= "LEFT JOIN #__joomleague_teams AS ref ON ref.id = m.referee_id ";
    
    $query .= " WHERE p.published = 1 
                AND ref.club_id = $clubId ";
    
    if ( $seasonId ) {
      $query .= " AND s.id = " . $seasonId;
    }
                 
    $query .= " ORDER BY m.match_date ASC ";

    $database->setQuery( $query );
    // echo $database->getQuery();
    return $database->loadObjectList();
  }
  
function showClubInfo($cid ,&$overall_config) {

  global $my, $database, $mainframe;
  global $joomleague;
  global $htmlHelper;
  
  $allowed=0;
  $showClub_config=$joomleague->getTemplateConfig("show_club.tpl");
  
  if($cid > 0){
    $club = new Club($database);
    $club->load($cid);
    $mainframe->setPageTitle(_JL_PAGE_TITLE_CLUB_INFO.' - '.$club->name);
//    $mainframe->appendPathWay(_JL_PAGE_TITLE_CLUB_INFO.' - '.$club->name);
  // now get all teams which belongs to this club (TODO: check published state of projects/seasons!!)
  $query = sprintf("SELECT id, name as team_name, short_name as team_shortcut, description as team_description 
                      FROM #__joomleague_teams
                     WHERE club_id = %s", $cid);
  
  $database->setQuery($query);
  // TODO: put result with this project ID first!!
  $rows = $database->loadObjectList();
  
  $teamIds = array();
  foreach ( $rows as $team) {
  	$teamIds[] = $team->id;
  }
  
  //playgrounds
  if ($club->standard_playground > 0) $stadiums[] = $club->standard_playground;
  //get playgrounds associated to teamtool belonging to team from club
	if (count($rows>0)) {
		foreach ($rows AS $row) {
			$query="SELECT distinct(standard_playground) FROM #__joomleague_team_joomleague
			         WHERE team_id = $row->id 
                  AND standard_playground > 0";
      if ($club->standard_playground >0 ) $query .= " AND standard_playground <> $club->standard_playground";
			$database->setQuery($query);
			if ($res = $database->loadResult()) $stadiums[] = $res;
		}
	}
	//editors      
  if ( defined('JL_USER_IS_BIG_BOSS')
      || ($my->id > 0 && $my->id == $club->admin) ) {
    $allowed=1;
  }
  $show_map = false;
  $address_string = $club->address." ".$club->zipcode." ".$club->location;
  if (isset($overall_config['google_api_key']) && $overall_config['google_api_key'] != '' &&
    isset($showClub_config['show_map']) && $showClub_config['show_map'] == 1 && trim($address_string) != '') {
      $show_map = true;
      $map_config=$joomleague->getTemplateConfig("map.tpl");
	    $gm = new EasyGoogleMap($overall_config['google_api_key'], 'jl_club_map');
	    $gm->SetMapDefaultType($map_config['default_map_type']);
		  $width = (is_int($map_config['width'])) ? $map_config['width'].'px' : $map_config['width'];
		  $gm->SetMapWidth($width);
		  $gm->SetMapHeight($map_config['height']); 
		  $gm->SetMapControl($map_config['map_control']);
		  if (intval($map_config['map_zoom'])>0) $gm->SetMapZoom(intval($map_config['map_zoom']));
		  $gm->mScale = (intval($map_config['map_scale'])>0) ? TRUE : FALSE;
		  $gm->mMapType = (intval($map_config['map_type_select'])>0) ? TRUE : FALSE;
		
		  $gm->mContinuousZoom = (intval($map_config['cont_zoom'])>0) ? TRUE : FALSE;
		  $gm->mDoubleClickZoom = (intval($map_config['dblclick_zoom'])>0) ? TRUE : FALSE;
		  $gm->mInset = (intval($map_config['map_inset'])>0) ? TRUE : FALSE;
		
		  $gm->mShowMarker = (intval($map_config['show_marker'])>0) ? TRUE : FALSE;
		  $gm->SetMarkerIconStyle($map_config['map_icon_style']);
		  $gm->SetMarkerIconColor($map_config['map_icon_color']);
		  $gm->SetAddress($address_string);
	}
	
    // for season calendar
    if ($showClub_config['show_club_season_calendar'] == "1") 
    {
      $seasonGames = getSeasonClubCalendar( $cid, $joomleague->season_id, $joomleague->teams_as_referees );
      
      if ( $joomleague->teams_as_referees ) {
        $refseasongames = getSeasonClubRefCalendar( $cid, $joomleague->season_id );       
      }
    }
  require(getTemplateFile(JL_TEMPLATE_PATH, 'show_club.tpl.php'));
  }
}

?>
